package org.mdrtwell.designpattern.behavioral.interpreter;

import java.util.Map;

interface Expression {
	public int interpret(Map<String, Expression> variables);
}

class Number implements Expression {
	private int	number;

	public Number(int number) {
		this.number = number;
	}

	public int interpret(Map<String, Expression> variables) {
		return number;
	}
}

class Plus implements Expression {
	Expression	leftOperand;
	Expression	rightOperand;

	public Plus(Expression left,
			Expression right) {
		leftOperand = left;
		rightOperand = right;
	}

	public int interpret(Map<String, Expression> variables) {
		return leftOperand.interpret(variables) + rightOperand.interpret(variables);
	}
}

class Minus implements Expression {
	Expression	leftOperand;
	Expression	rightOperand;

	public Minus(Expression left,
			Expression right) {
		leftOperand = left;
		rightOperand = right;
	}

	public int interpret(Map<String, Expression> variables) {
		return leftOperand.interpret(variables) - rightOperand.interpret(variables);
	}
}

class Variable implements Expression {
	private String	name;

	public Variable(String name) {
		this.name = name;
	}

	public int interpret(Map<String, Expression> variables) {
		if (null == variables.get(name))
			return 0; // Either return new Number(0).
		return variables.get(name).interpret(variables);
	}
}
